﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml;
using System.Configuration;

namespace ServicioCliente
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IServicioCliente
    {
        public ClienteRetorno ObtenerCliente(int idCliente)
        {
            XmlDocument inventory = new XmlDocument();
            string path = ConfigurationManager.AppSettings["XmlPath"];
            inventory.Load(path);

            XmlNodeList elements = inventory.SelectNodes("//Clientes//Cliente");
            ClienteRetorno c = new ClienteRetorno();

            foreach (XmlElement element in elements)
            {
                int id = Int32.Parse(element.GetAttribute("id"));
                if (id == idCliente)
                {
                    c.Nombre = element.GetAttribute("nombre");
                    c.Identificador = idCliente;
                    c.Saldo = Int32.Parse(element.GetAttribute("saldo"));
                    c.TipoCliente = element.GetAttribute("tipoCliente");

                    //XmlDocument cobros = new XmlDocument();
                    //cobros.LoadXml(element.InnerXml);
                    if (element.HasChildNodes)
                    {
                        //XmlNodeList elementosCobro = cobros.SelectNodes("//Consumo");
                        XmlNodeList elementosCobro = element.ChildNodes;

                        foreach (XmlElement consumo in elementosCobro)
                        {
                            if (c.Consumos == null)
                                c.Consumos = new List<int>();
                            c.Consumos.Add(Int32.Parse(consumo.InnerText));
                        }
                    }

                    break;
                }
            }
            return c;

        }
    }
}
